﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;
using System.Text;

namespace CRM.Web.SurveyManage
{
    public partial class SurveyItems : Common.PageBase
    {
        public string ModifyUrl = "SurveyItems.aspx?AUTOID={0}&OPAUTOID={1}";
        T_SURVEY_ITEMS_BLL itemsBll = new T_SURVEY_ITEMS_BLL();
        T_SURVEY_OPTIONS_BLL optionsBll = new T_SURVEY_OPTIONS_BLL();

        protected override void OnInit(EventArgs e)
        {
            base.VerificationRight = false;
            base.OnInit(e);
            gvList.PageSize = 10;
            gvList_Options.PageSize = 10;
            PageHelper.SetPrePageUrl(this.Page);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                EnumHelper.FillListControl(ddlItem_Type, typeof(ItemType));
                string autoid = PageHelper.GetQueryString(this.Page, "AUTOID");
                string opautoid = PageHelper.GetQueryString(this.Page, "OPAUTOID");
                pageDataInit(autoid, opautoid);
                if (!String.IsNullOrEmpty(autoid))
                    gvDataBinding(" AND ITEM_ID='" + autoid + "'");
                else
                    gvDataBinding("");
            }
        }

        private void pageDataInit(string autoid, string opautoid)
        {
            if (!String.IsNullOrEmpty(autoid))
            {
                T_SURVEY_ITEMS_Entity entity = new T_SURVEY_ITEMS_Entity();
                entity = itemsBll.GetEntity("AUTOID", StringHelper.ToInt(autoid));
                if (entity == null) return;
                PageHelper.SetControlValue(this.plItems, entity);
            }
            if (!String.IsNullOrEmpty(opautoid))
            {
                T_SURVEY_OPTIONS_Entity entity = new T_SURVEY_OPTIONS_Entity();
                entity = optionsBll.GetEntity("AUTOID", StringHelper.ToInt(opautoid));
                if (entity == null) return;
                PageHelper.SetControlValue(this.plOptions, entity);
                txtITEM_NAME1.Text = txtItem_Name.Text;
            }
        }

        /// <summary>
        /// 页面数据绑定
        /// </summary>
        private void gvDataBinding(string OptionSql)
        {
            #region 查询条件字符串
            StringBuilder sql = new StringBuilder();
            #endregion

            List<T_SURVEY_ITEMS_Entity> list;
            //获取总的记录条数
            gvList.RecordCount = (int)itemsBll.GetCount(sql.ToString());

            list = itemsBll.GetList(sql.ToString(), gvList.CurrentPageIndex * gvList.PageSize, gvList.PageSize, gvList.SortConditions);
            gvList.DataSource = list;
            gvList.DataKeyNames = new string[] { "AUTOID" };
            gvList.DataBind();


            List<T_SURVEY_OPTIONS_Entity> listOptions;
            gvList_Options.RecordCount = (int)optionsBll.GetCount(OptionSql);
            listOptions = optionsBll.GetList(OptionSql, gvList_Options.CurrentPageIndex * gvList_Options.PageSize, gvList_Options.PageSize, gvList_Options.SortConditions);
            gvList_Options.DataSource = listOptions;
            gvList_Options.DataKeyNames = new string[] { "AUTOID" };
            gvList_Options.DataBind();
        }

        /// <summary>
        /// 新建题目
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (sender == btnAdd)
            {
                txtAutoid.Text = "";
                txtItem_Name.Text = "";
                ddlItem_Type.SelectedValue = ((int)ItemType.单选).ToString();
                rbENINPUT0.Checked = true;
            }
            else if (sender == btnAdd_OPTIONS)
            {
                string optionid = optionsBll.GetMaxValue("OPTION_ID", " AND ITEM_ID='" + txtAutoid.Text + "'");
                int i = 1;
                if (!String.IsNullOrEmpty(optionid))
                {
                    i = StringHelper.ToInt(optionid) + 1;
                }
                txtOPTION_ID.Text = i.ToString() ;
                txtOPTION_NAME.Text = "";
                txtITEM_ID.Text = txtAutoid.Text;
                txtITEM_NAME1.Text = txtItem_Name.Text;
            }
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (sender == this.btnOK)
            {
                T_SURVEY_ITEMS_Entity entity = new T_SURVEY_ITEMS_Entity();
                if(txtItem_Name.Text=="")
                {
                    PageHelper.ShowMessage(this.Page, "题目名称不能为空！");
                    return;
                }
                entity = itemsBll.GetEntity("AUTOID", txtAutoid.Text);
                if (entity == null)
                {
                    entity = itemsBll.GetEntity("ITEM_NAME", txtItem_Name.Text);
                    if (entity == null)
                    {
                        entity = new T_SURVEY_ITEMS_Entity();
                        PageHelper.GetControlValue<T_SURVEY_ITEMS_Entity>(this.plItems, ref entity);
                        entity.ITEM_TYPE = StringHelper.ToInt(ddlItem_Type.SelectedValue);
                        itemsBll.Insert(entity);
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "题目已经存在，请核实后再进行增加");
                        return;
                    }
                }
                else
                {
                    PageHelper.GetControlValue<T_SURVEY_ITEMS_Entity>(this.plItems, ref entity);
                    entity.ITEM_TYPE = StringHelper.ToInt(ddlItem_Type.SelectedValue);
                    itemsBll.Update(entity);
                }
                PageHelper.ShowMessage(this.Page, "保存成功！");
                Response.Redirect(HttpContext.Current.Request.RawUrl);
            }
            else if (sender == this.btnOK_OPTIONS)
            {
                if (txtOPTION_ID.Text == "")
                {
                    PageHelper.ShowMessage(this.Page, "题目选项编号不能为空！");
                    return;
                }
                if (txtOPTION_NAME.Text == "")
                {
                    PageHelper.ShowMessage(this.Page, "题目选项名称不能为空！");
                    return;
                }
                List<T_SURVEY_OPTIONS_Entity> list = new List<T_SURVEY_OPTIONS_Entity>();
                T_SURVEY_OPTIONS_Entity entity = new T_SURVEY_OPTIONS_Entity();
                list = optionsBll.GetList(string.Format(" AND OPTION_ID='{0}' AND ITEM_ID='{1}'", txtOPTION_ID.Text, txtITEM_ID.Text));
                if (list.Count == 0)
                {
                    PageHelper.GetControlValue<T_SURVEY_OPTIONS_Entity>(this.plOptions, ref entity);
                    optionsBll.Insert(entity);
                }
                else
                {
                    entity.AUTOID = list[0].AUTOID;
                    PageHelper.GetControlValue<T_SURVEY_OPTIONS_Entity>(this.plOptions, ref entity);
                    optionsBll.Update(entity);
                }
                PageHelper.ShowMessage(this.Page, "保存成功！");
                Response.Redirect(HttpContext.Current.Request.RawUrl);
            }
        }

        #region GridView 事件
        /// <summary>
        /// GridView 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (sender == this.gvList)
            {
                T_SURVEY_ITEMS_Entity entity = new T_SURVEY_ITEMS_Entity();
                entity.AUTOID = int.Parse((sender as GridView).DataKeys[e.RowIndex].Values[0].ToString());
                long rel = itemsBll.Delete(entity);
                if (rel > 0)
                {
                    Response.Redirect(HttpContext.Current.Request.RawUrl);
                }
                else
                {
                }
            }
            else if (sender == this.gvList_Options)
            {
                T_SURVEY_OPTIONS_Entity entity = new T_SURVEY_OPTIONS_Entity();
                entity.OPTION_ID = int.Parse((sender as GridView).DataKeys[e.RowIndex].Values[0].ToString());
                long rel = optionsBll.Delete(entity);
                if (rel > 0)
                {
                    Response.Redirect(HttpContext.Current.Request.RawUrl);
                }
                else
                {
                }
            }
        }

        /// <summary>
        /// GridView 行数据绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (sender == this.gvList)
            {
                string temp = string.Empty;
                switch (e.Row.RowType)
                {
                    case DataControlRowType.DataRow:
                        {
                            GridView gv = (sender as GridView);
                            for (int i = 0; i < gv.Columns.Count; i++)
                            {
                                TemplateField tf = gv.Columns[i] as TemplateField;
                                if (tf != null)
                                {
                                    #region 存储值 转换成  显示值

                                    switch (tf.HeaderText)
                                    {
                                        case "操作":
                                            {
                                                e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                            }
                                            break;
                                        default:
                                            {
                                                e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                            }
                                            break;

                                    }

                                    #endregion
                                }

                                BoundField bf = gv.Columns[i] as BoundField;
                                if (bf != null)
                                {
                                    #region 存储值 转换成  显示值

                                    switch (bf.DataField)
                                    {
                                        case "ITEM_TYPE":
                                            try
                                            {
                                                temp = e.Row.Cells[i].Text.Trim();
                                                temp = ((ItemType)Enum.Parse(typeof(ItemType), temp)).ToString();
                                                e.Row.Cells[i].Text = temp;
                                            }
                                            catch (Exception)
                                            {

                                                //throw;
                                            }
                                            break;
                                        default:
                                            {

                                            }
                                            break;
                                    }

                                    #endregion
                                }
                            }
                        }
                        break;
                    case DataControlRowType.Header:
                        {

                        }
                        break;
                    case DataControlRowType.Pager:
                        {

                        }
                        break;
                    case DataControlRowType.EmptyDataRow:
                        {


                        }
                        break;
                    default:

                        break;
                }
            }
            else if (sender == this.gvList_Options)
            {
                string temp = string.Empty;
                switch (e.Row.RowType)
                {
                    case DataControlRowType.DataRow:
                        {
                            GridView gv = (sender as GridView);
                            for (int i = 0; i < gv.Columns.Count; i++)
                            {
                                TemplateField tf = gv.Columns[i] as TemplateField;
                                if (tf != null)
                                {
                                    #region 存储值 转换成  显示值

                                    switch (tf.HeaderText)
                                    {
                                        case "操作":
                                            {
                                                e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                            }
                                            break;
                                        default:
                                            {
                                                e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                            }
                                            break;

                                    }

                                    #endregion
                                }

                                BoundField bf = gv.Columns[i] as BoundField;
                                if (bf != null)
                                {
                                    #region 存储值 转换成  显示值                                  

                                    #endregion
                                }
                            }
                        }
                        break;
                    case DataControlRowType.Header:
                        {

                        }
                        break;
                    case DataControlRowType.Pager:
                        {

                        }
                        break;
                    case DataControlRowType.EmptyDataRow:
                        {


                        }
                        break;
                    default:

                        break;
                }
            }

        }

        /// <summary>
        /// GridView 行创建事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {

                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }
        #endregion
    }
}
